package com.txc.controller;

import com.txc.mapper.RootMapper;
import com.txc.mapper.UserMapper;
import com.txc.pojo.Root;
import com.txc.pojo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.omg.CORBA.UnknownUserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.Collection;
import java.util.List;

@Controller
public class helloController {

  @Autowired
  private UserMapper userMapper;
  @Autowired
  private RootMapper rootMapper;

  //跳转到注册页面
  @RequestMapping("/toSign")
    public String toSign(){
      return "sign";
  }
  //跳转到主页面
  @RequestMapping("/toIndex")
  public String toIndex(){
    return "index";
  }

  //跳转到登陆页面
  @RequestMapping("/toLogin")
  public String toLogin(){
    return "login";
  }

  @RequestMapping("/toRootLogin")
  public String toRootLogin(){
    return "rootindex";
  }

  @RequestMapping("/toDashboard")
  public String toDashboard(){
    return "dashboard";
  }

  @RequestMapping("/user/login")
  public String userLogin(String username, String password, Model model, HttpSession session){
    //默认管理员用户名为root,密码为root123
    //获得当前对象
    Subject subject = SecurityUtils.getSubject();
    //将用户放入session
    session.setAttribute("RootSession",username);
    //封装当前的用户对象
    UsernamePasswordToken token = new UsernamePasswordToken(username,password);
    try {
      subject.login(token);//执行登录方法，如果没有异常则说明登录成功
      model.addAttribute("name",username);
      return "dashboard";
    }catch (UnknownAccountException uae){
      model.addAttribute("msg","用户名或密码错误");//用户名错误
      return "rootindex";
    }catch (IncorrectCredentialsException ice){
      model.addAttribute("msg","用户名或密码错误");//密码错误
      return "rootindex";
    }
  }

//未授权无法进行管理 只有Root用户且status码为1才可以对用户进行管理
   @RequestMapping("/noauth")
   @ResponseBody
   public String noauth(){
     return "未授权，无法管理！";
  }

  //跳到添加用户列表
  @RequestMapping("/user")
  public String list(Model model){
    List<User> users = userMapper.queryAll();
    model.addAttribute("USERS",users);
    return "users/list";
  }
//跳到添加用户页面
  @GetMapping("/toAdd")
  public String toadd(){
    return "users/add";
  }
//添加新用户
  @PostMapping("/toAdd")
  public String Add(String userName,String pwd,String email){
    User user = new User(userName, pwd, email);
    userMapper.addUser(user);
    return "redirect:/user";
  }
  //跳到修改页面
  @GetMapping("/toUpdate/{id}")
  public String update(@PathVariable("id")Integer id,Model model){
    //查出原来的数据
    User user = userMapper.queryOne(id);
    model.addAttribute("users",user);
    return "users/update";
  }
//修改用户信息
  @PostMapping("/toUpdate")
  public String Update(int id,String userName,String pwd,String email){
    User user = new User(id, userName, pwd, email);
    System.out.println(user);
    userMapper.updateUser(user);
    return "redirect:/user";
  }

  //删除用户信息
  @RequestMapping("/toDelete/{id}")
  public String delete(@PathVariable("id")Integer id,Model model){
    userMapper.deleteUser(id);
    model.addAttribute("dltmsg","删除成功!");
    return "redirect:/user";
  }

  //注销
  @RequestMapping("/logout")
  public String signout(HttpSession session){
    session.removeAttribute("usersession");
    return "redirect:/toRootLogin";
  }
}
